import waterMark from '@/utils/waterMark';
import { getUserInfoApi } from '@/api/common';
import { defineStore } from 'pinia';

interface UserState {
  info?: UserInfomation;
}

const useUserStore = defineStore('user', {
  state: (): UserState => ({
    info: undefined
  }),
  actions: {
    async getUserInfo() {
      try {
        const data = await getUserInfoApi();
        const { username = '', phone = '' } = data;
        waterMark.setTarget(document.getElementById('app'));
        waterMark.setCtx(`${username}${phone.slice(-4)}` || '');
        this.info = data;
      } catch (error) {
        waterMark.destroy();
        return Promise.reject(error);
      }
    }
  }
});

export default useUserStore;
